// components/number-data/index.js
import {
  list
} from '../../api/number-select'
import {
  list as _list
} from '../../api/number'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    type: {
      type: String,
      value: '',
      observer(data) {
        if (!!data) {
          this.fetchData(data);
        }
      }
    },
    dataSource: {
      type: Array,
      value: [],
      observer(data) {
        if (data && data.length > 0) {
          setTimeout(() => {
            this.setSelectData(data);
          }, 600)
        }
      }
    },
    disabled: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    numSettings: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    fetchData(data) {
      _list({
        numSettingsType: data
      }, res => {
        this.setSelectData(res);
      })
    },
    getIds(data) {
      
      return data.filter(it => it.valueType === 'SELECT').map(it => it.id).join(',')
    },
    async setSelectData(data) {
      
        const d = await this.fetchSelectData(this.getIds(data));
        const _d = data.concat([]);
        for (let it of _d) {
          it._selectData = [];
          it.numSelectManageId = '';
          for (let t of d) {
            if (t.numSettingsId === it.id) {
              it._selectData.push({
                name: t.value,
                value: t.id
              });
            }
          }
        }

        this.setData({
          numSettings: _d
        })
    
    },
    fetchSelectData(id) {
        return new Promise(resolve => {
          list({
            numSettingsId: id
          }, resolve)
        })
    
    },
    onFocus() {
      this.triggerEvent('focus');
    },
    onBlur() {
      this.triggerEvent('blur');
    },
    onFieldChange(event) {
      const index = event.currentTarget.dataset.index,
        valueType = event.currentTarget.dataset.valuetype;
      const numSettings = this.data.numSettings;
      const value = event.detail;
      numSettings[index].value = value;
      numSettings[index].valueType = valueType;
      this.setData({
        numSettings
      })
    },
    onSelectChange(event) {
      const index = event.currentTarget.dataset.index,
        valueType = event.currentTarget.dataset.valuetype;
      const numSettings = this.data.numSettings;
      const value = event.detail[0];

      numSettings[index].numSelectManageId = value;
      numSettings[index].valueType = valueType;

      this.setData({
        numSettings
      })
    },
    bindDateChange(event) {
      const index = event.currentTarget.dataset.index,
        valueType = event.currentTarget.dataset.valuetype;
      const numSettings = this.data.numSettings;
      const value = event.detail.value;
      numSettings[index].value = value;
      numSettings[index].valueType = valueType;
      this.setData({
        numSettings
      })
    },
    getDataSource() {
      return this.data.numSettings
    },
    clearDate(event) {
      const index = event.currentTarget.dataset.index;
      const numSettings = this.data.numSettings;
      numSettings[index]['value'] = null;
      this.setData({
        numSettings
      })
    }
  },
  lifetimes: {

  }
})